package com.right.health.app.ui.me;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.AddressListResponse;
import com.right.health.app.dialog.AddressDeleteDialog;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.me.adapter.ShippingAddressAdapter;

/**
 * @author: weming
 * @date: 2021/3/28
 * @description:收货地址
 */
public class ShippingAddressActivity extends BaseActivity implements OnHttpListener,View.OnClickListener {
    private static final int HTTP_CODE_GET_ADDRESS = 1;
    private static final int HTTP_CODE_DELETE_ADDRESS = 2;
    private static final int HTTP_CODE_MODIFY_ADDRESS = 3;

    private Button mAddBtn;
    private ListView mListView;
    private ShippingAddressAdapter mAdapter;
    private View mEmptyView;
    private UserApi mUserApi;
    private AddressDeleteDialog mDeleteDialog;
    private ProgressDialog mProgressDialog;
    private boolean mIsSelect;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_shipping_address);
    }

    @Override
    public void findView() {
        mAddBtn = findViewById(R.id.btn_add);
        mListView = findViewById(R.id.listView);
        mEmptyView = findViewById(R.id.empty_view);
        mListView.setEmptyView(mEmptyView);
        mAdapter = new ShippingAddressAdapter(this);
        mAdapter.setOnClickListener(this);
        mListView.setAdapter(mAdapter);
        mAddBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, EditAddressActivity.class);
            startActivityForResult(intent,1000);
        });
        mListView.setOnItemClickListener((parent, view, position, id) -> {
            if (mIsSelect) {
                Intent intent = new Intent();
                intent.putExtra("result", mAdapter.getItem(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void initialize() {
        mIsSelect = getIntent().getBooleanExtra("isSelect", false);
        mUserApi = new UserApi();
        mUserApi.setListener(this);
        mUserApi.addRequestCode(HTTP_CODE_GET_ADDRESS);
        mProgressDialog = ProgressDialog.show(this, "", "加载中",false,true);
        mUserApi.getShippingAddress();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            if (data.getBooleanExtra("refresh",false)){
                mUserApi.addRequestCode(HTTP_CODE_GET_ADDRESS);
                mUserApi.getShippingAddress();
            }
        }
    }

    @Override
    public void onClick(View v) {
        try {
            switch (v.getId()) {
                case R.id.tv_delete:
                    showDeleteDialog((int)v.getTag());
                    break;
                case R.id.tv_edit:
                    EditAddressActivity.goEdit(this,mAdapter.getItem((int)v.getTag()));
                 break;
                case R.id.iv_check:
                case R.id.tv_default:
                int index=(int)v.getTag();
                    if (index != mAdapter.getDefaultIndex()) {
                        mUserApi.setTag(index);
                        mUserApi.addRequestCode(HTTP_CODE_MODIFY_ADDRESS);
                        AddressListResponse.UserAddressBean bean = mAdapter.getItem(index);
                        mUserApi.modifyShippingAddress(bean.getAddressid(),bean.getUsername(),bean.getUserphone()
                                ,bean.getProvinceid(),bean.getCityid(),bean.getCountyid(),bean.getUseraddress(),"1");
                    }
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void showDeleteDialog(int position) {
        if (mDeleteDialog==null) {
            mDeleteDialog = new AddressDeleteDialog(this);
            mDeleteDialog.setDelegate(dialog -> {
                mUserApi.setTag(position);
                mUserApi.addRequestCode(HTTP_CODE_DELETE_ADDRESS);
                mUserApi.deleteShippingAddress(mAdapter.getItem(position).getAddressid());
                dialog.dismiss();
            });
        }
        mDeleteDialog.show();
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            if (mProgressDialog != null) {
                mProgressDialog.dismiss();
            }
            ToastUtil.show(this, response.error);
        } else {
            if (HTTP_CODE_GET_ADDRESS == request.requestCode) {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
                try {
                    AddressListResponse listResponse=response.cast(AddressListResponse.class);
                    mAdapter.setDataSource(listResponse.getUserAddress());
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.show(this,"数据解析失败");
                }
            } else if (HTTP_CODE_DELETE_ADDRESS == request.requestCode) {
                ToastUtil.show(this, "删除成功");
                try {
                    mAdapter.removeItem((int)request.tag);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (HTTP_CODE_MODIFY_ADDRESS == request.requestCode) {
                try {
                    int beforeIndex = mAdapter.getDefaultIndex();
                    if (beforeIndex != -1) {
                        mAdapter.getItem(beforeIndex).setIsdefault("0");
                    }
                    mAdapter.getItem((int)request.tag).setIsdefault("1");
                    mAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

